<?php
/**
 * 
 * +------------------------------------------------------------+
 * @category App 
 * +------------------------------------------------------------+
 * 框架核心类，完成对所有控制器的调度
 * +------------------------------------------------------------+
 *
 * @author anzai <236049524@qq.com>
 * @copyright http://www.judianyun.com 2013
 * @version 1.0
 *
 * Modified at : 2013-2-17 10:06:03
 *
 */
class App extends Base {
	public function __construct(){
		parent::__construct();
		
		//注册AUTOLOAD方法
        spl_autoload_register(array('App', '_autoload'));
		
        //自动创建模块存放目录
		if (!is_dir(LIB_PATH)){
			Helper::mkdir(LIB_PATH);
			$actionCode = '<?php'."\n".' class indexAction extends Action{'."\n".'	function index(){'."\n".'		echo "<center style=\'font-size:2.5em;font-family:微软雅黑;\'><b><span style=\'font-size:3.5em;\'>:)</span><br/>EasyPHP让您的编程变得如此的简单！！</b></center>";'."\n".'	}'."\n".'}';
			Helper::write(LIB_PATH . 'index/indexAction.class.php', $actionCode);
			unset($actionCode);
		}
		
		if(C('SESSION_START')){
			$session_name = session_name();
			$session_id = $session_name ? $this->params->$session_name : false;
			if($session_id){
				session_id($session_id);
			}
			session_start();
		}
	}
	
	//执行项目
	public function run(){
		Helper::_running();
		if(APP_DEBUG){
			//显示调试信息
			$this->_debug();
		}
	}
	
	//设置数据缓存配置信息，一个数组
	public function setCache($config){
		if($config){
			C('CACHE_CONFIG', $config);
		}
		return $this;
	}
	
	//设置数据库配置信息，一个数组
	public function setDb($dbConfig){
		if($dbConfig){
			C('DB_CONFIG', $dbConfig);
		}
		return $this;
	}
	
	//设置模板配置信息，一个数组
	public function setView($viewConfig){
		if($viewConfig){
			C('VIEW_CONFIG', $viewConfig);
		}
		return $this;
	}
	
	
	///自动加载类文件
	static public function _autoload($class){
		if (!class_exists($class)){
			$clsfile = $class . '.class.php';
			if(defined('ACTION_PATH') && is_file(ACTION_PATH . $clsfile)){
				require_once ACTION_PATH . $clsfile;
				return true;
			}
			
			if (MODEL_SUFFIX == substr($class, -strlen(MODEL_SUFFIX))){
				$modDir = substr($class, 0, -strlen(MODEL_SUFFIX)) . DS;
			}elseif (ACTION_SUFFIX == substr($class, -strlen(ACTION_SUFFIX))){
				$modDir = substr($class, 0, -strlen(ACTION_SUFFIX)). DS;
			}
			
			$dirs[] = LIB_PATH;				//控制器主目录
			if (!defined('SUB_LIB_PATH') || SUB_LIB_PATH == ''){
				$dirs[] = LIB_PATH . $GLOBALS['MODULE_NAME'] . DS;
				$modDir AND $dirs[] = LIB_PATH . $modDir;
			}else{
				$subdir = (is_dir(SUB_LIB_PATH) ? '' : LIB_PATH) . SUB_LIB_PATH;
				$dirs[] = $subdir;
				$dirs[] = $subdir . $GLOBALS['MODULE_NAME'] . DS;
				$modDir AND $dirs[] = $subdir . $modDir;
				unset($subdir);
			}
			
			//从自定义加载类库目录寻找文件
			$auto_dir = C('AUTOLOAD_DIR');
			if (false !== $auto_dir && !empty($auto_dir)){
				$auto_dir = is_array($auto_dir) ? $auto_dir : array($auto_dir);
				foreach ($auto_dir as $dir){
					$dir = format_dir($dir . '/');
					if (is_dir($dir)){
						$dirs[] = $dir;
						$modDir AND $dirs[] = $dir . $modDir;
					}else{
						$dirs[] = LIB_PATH . $dir;
						$dirs[] = LIB_PATH . $dir . $GLOBALS['MODULE_NAME'] . DS;
						$modDir AND $dirs[] = LIB_PATH . $dir . $modDir;
					}
				}
			}
			
			$dirs = array_unique($dirs);
			
			foreach ($dirs as $dir){
				if (is_dir($dir) && is_file($f = format_dir($dir . '/') . $clsfile)){
					include_once $f;
					if (!defined('ACTION_PATH') && false !== stripos($f, $GLOBALS['MODULE_NAME'] . ACTION_SUFFIX . '.class.php')){
						define('ACTION_PATH', dirname($f) . DS);
					}
					return true;
				}
			}
		}
		
		return false;
	}
	
	private function _debug(){
		$runtime = number_format(microtime(TRUE) - $GLOBALS['__StartTime__'], 6) . 's';
		$out = $this->_out($_GET + $_POST);
		$dbQuerys = $GLOBALS['DB_QUERYS'];
		require_once __EXCEPTION_PATH__ . 'running.html';
	}
	
	private function _out($var){
		if (empty($var))return;
		foreach ($var as $k => $v){
			$out .= '<pre>' . $k . ' => ';
			is_array($v) ? $out .= 'array(<br/>' . $this->_out($v) . ')' : $out .= htmlspecialchars($v, ENT_QUOTES, OUT_CHARSET);
			$out .= '</pre>';
		}
		return $out;
	}
	
	public function redirect($url, $filter = array()){
		$b = true;
		if ($filter){
			foreach ($filter as $u){
				if (false !== stripos(HTTP_URL, $u)){
					$b = false;
					break;
				}
			}
		}
		if ($b && false === stripos(HTTP_URL, $url)){
			Helper::redirect($url . url(ACTION_NAME, MODULE_NAME, $_GET));
		}
	}
}

/**
 * 
 * +------------------------------------------------------------+
 * @category Factory 
 * +------------------------------------------------------------+
 * 总调度工厂类，调用Cache、数据库、模板引擎总入口
 * +------------------------------------------------------------+
 *
 * @author anzai <236049524@qq.com>
 * @copyright http://www.judianyun.com 2012
 * @version 1.0
 *
 * Modified at : 2012-8-9 10:19:43
 *
 */
class Factory extends Base {
	static function cache($config = null){
		$config = empty($config) ? C('CACHE_CONFIG') : $config;
		require_once __CACHE_PATH__ . 'Cache.class.php';
		switch (strtolower($config['type'])){
			case 'file':
				$engine = 'FCache';
				break;
			default:
				$engine = 'MCache';
		}
		
		require_once __CACHE_PATH__ . $engine . '.class.php';
		return new $engine($config);
	}
	
	static function db($config = null, $reflection = array()){
		$config = empty($config) ? C('DB_CONFIG') : $config;
		empty($config) && E::halt(Helper::L('CLS_F_DB'));
		
		require_once __DB_PATH__ . 'Db.class.php';
		switch (strtolower($config['dbtype'])){
			case 'mysqli':
				$engine = 'MysqliDb';
				break;
			case 'mysqlpdo':
				$engine = 'MysqlpdoDb';
				break;
			case 'mssql':
				$engine = 'MssqlDb';
				break;
			case 'oracle'://oracle数据库
				$engine = 'OracleDb';
				break;
			default://默认使用mysql数据库驱动类
				$engine = 'MysqlDb';
				break;
		}
		require_once __DB_PATH__ . $engine . '.class.php';
		return new $engine($config, $reflection);
	}
	
	static function view($config=''){
		$config = empty($config) ? C('VIEW_CONFIG') : $config;
		empty($config) && E::halt(Helper::L('CLS_F_VIEW'));
		require_once __VIEW_PATH__ . 'View.class.php';
		$config['type'] = isset($config['type']) ? $config['type'] : 'easy';
		switch (strtolower($config['type'])){
			case 'smarty'://Smarty模板引擎
				$engine = 'SmartyView';
				break;
			case 'discuz'://将discuz模板引擎进行类封装后的模板引擎
				$engine = 'DiscuzView';
				break;
			default://默认使用内置模板引擎
				$engine = 'EasyView';
		}
		
		require_once __VIEW_PATH__ . $engine . '.class.php';
		
		return new $engine($config);
	}
}
?>